package com.lws.basic.utils.common;


import java.text.DecimalFormat;
import java.util.List;

/**
 * @松 on 2019/7/30 13:38
 * 经纬度之间的距离
 */
public class DistanceUtils {
    /**
     * 地球半径,单位 km
     */
    private static final double EARTH_RADIUS = 6378.137;

    /**
     * 根据经纬度，计算两点间的距离
     *
     * @param longitude1 第一个点的经度
     * @param latitude1  第一个点的纬度
     * @param longitude2 第二个点的经度
     * @param latitude2  第二个点的纬度
     * @return 返回距离 单位米
     */
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 纬度
        double lat1 = Math.toRadians(latitude1);
        double lat2 = Math.toRadians(latitude2);
        // 经度
        double lng1 = Math.toRadians(longitude1);
        double lng2 = Math.toRadians(longitude2);
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lng1 - lng2;
        // 计算两点距离的公式
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘地球半径, 返回单位: 千米
        s =  s * EARTH_RADIUS *1000;
        return s;
    }


    //保留10位小数
    public static double decimalSeven(Double d){
        DecimalFormat df = new DecimalFormat("#.0000000000");
        String str = df.format(d);
        return Double.valueOf(str);

    }
    //保留2位小数
    public static double decimalTwo(Double d){
        DecimalFormat df = new DecimalFormat("#.00");
        String str = df.format(d);
        return Double.valueOf(str);

    }
    //保留4位小数
    public static double decimalFour(Double d){
        DecimalFormat df = new DecimalFormat("#.0000");
        String str = df.format(d);
        return Double.valueOf(str);

    }



  /*  //多项式 曲线拟合
    public static List<PointBean>  CurveFitter(List<PointBean> pointBeans, int n) {
        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (PointBean p : pointBeans){
                obs.add(p.getLat(), p.getLon());

        }


        // Instantiate a third-degree polynomial fitter.
        final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(n);

        // Retrieve fitted parameters (coefficients of the polynomial function).
        double[] coeff = fitter.fit(obs.toList());
        for (double c : coeff) {
            System.out.println(c+"拟合"+coeff.length);
       }

        if ( n ==2){
            for (int i=0;i<pointBeans.size();i++){
                System.out.println("经度前"+pointBeans.get(i).getLon()+"----"+pointBeans.get(i).getLat());
                pointBeans.get(i).setLon(getY2(pointBeans.get(i).getLat(),coeff ));
                System.out.println("经度后"+pointBeans.get(i).getLon()+"----"+pointBeans.get(i).getLat());

            }
        }
        if (n==3) {
            for (int i=0;i<pointBeans.size();i++){
                System.out.println("经度前"+pointBeans.get(i).getLon()+"----"+pointBeans.get(i).getLat());
                pointBeans.get(i).setLon(getY3(pointBeans.get(i).getLat(),coeff ));
                System.out.println("经度后"+pointBeans.get(i).getLon()+"----"+pointBeans.get(i).getLat());

            }
        }
        if (n==1) {
            for (int i=0;i<pointBeans.size();i++){
                System.out.println("经度前"+pointBeans.get(i).getLon()+"----"+pointBeans.get(i).getLat());
                pointBeans.get(i).setLon(getY(pointBeans.get(i).getLat(),coeff ));
                System.out.println("经度后"+pointBeans.get(i).getLon()+"----"+pointBeans.get(i).getLat());

            }
        }

        return pointBeans;
    }
*/
    //y = ax² + bx +c
  /*  public static double getY(double x,double[] d){
        return (d[0]*x) + (d[1]);

    }*/
    //y = ax² + bx +c
    public static double getY(double x,double[] d){
        return decimalSeven((d[1]*x) + (d[0]));

    }

    //y = ax² + bx +c
    public static double getY2(double x,double[] d){
          return decimalSeven((d[2]*(x*x)) + (x*d[1]) + (d[0]));

    }
    //y = ax² + bx +c
    public static double getY3(double x,double[] d){
        return decimalSeven((d[3]*(x*x*x)) + ((x*x )*d[2]) + (d[1]*x) + d[0]);

    }


}
